package com.lins.store.controller;

import com.lins.store.entity.FileEntity;
import com.lins.store.entity.TreeEntity;
import com.lins.store.entity.ex.FileStateException;
import com.lins.store.bo.FileService;
import com.lins.common.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 *
 * @Author: linshuo
 * @Date: 2022/9/28 14:28
 **/

@RestController
@RequestMapping("fileList")
@Api(value = "文件接口",tags = {"文件接口"}) //接口名称和标签名
public class FileController extends BaseController {
    @Autowired
    private FileService fileService;

    /*@Autowired
    private OSS ossClient;*/

    /**
     * 文件列表
     * @param session
     * @return
     */
    @GetMapping({"", "/"}) //接口文档会生成两个
    //@GetMapping("/") //可能导致url拼接时异常
    @ApiOperation("跟据用户id获取文件信息列表")
    public JsonResult<List<FileEntity>> getByUid(HttpSession session) {
        //需要先判断该用户是否存在
        //log.warn("需要先判断该用户是否存在");
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("parUrl","/home");
        return new JsonResult<>(OK,  fileService.findList(getUidFromSession(session),paramMap));
    }

    /**
     * 检索文件
     * @param paramMap
     * @param session
     * @return
     */
    @PostMapping("/search")
    @ApiOperation("检索文件")
    public JsonResult<List<FileEntity>> search(@RequestParam Map<String, String> paramMap,HttpSession session) {
        return new JsonResult<>(OK,fileService.search(getUidFromSession(session),paramMap));
    }

    /**
     * 根据uid和父目录查列表(点进文件夹时，根据本接口获取文件夹内文件列表)
     * @param fileEntity
     * @param session share=03查别人的共享
     * @return
     */
    @GetMapping("urlList")
    @ApiOperation("根据父url获取用户权限下的文件信息列表")
    public JsonResult<List<FileEntity>> urlList(FileEntity fileEntity, HttpSession session) {
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("parUrl",fileEntity.getUrl());
        paramMap.put("share",fileEntity.getShare());
        return new JsonResult<>(OK,fileService.findList(getUidFromSession(session),paramMap));
    }

    /**
     * 获取移动文件目录树
     * @param session
     * @return
     */
    @GetMapping("jsonTree")
    @ApiOperation("获取移动文件目录树")
    public JsonResult<List<TreeEntity>> jsonTree(HttpSession session) {
        return new JsonResult<>(OK, fileService.jsonTree(getUidFromSession(session)));
    }

    /**
     * 上传的文件存放OSS
     * @param file
     * @param paramMap
     * @param session
     * @return
     */
    @PostMapping("fileUpload")
    @ApiOperation("上传文件到OSS")
    public JsonResult<String> fileUpload(@RequestParam("file") MultipartFile file,
                                         @RequestParam Map<String, String> paramMap,
                                         HttpSession session) {
        String s = fileService.fileUpload(getUidFromSession(session),file,paramMap);
        return new JsonResult<>(OK, s);
    }

    /**
     * 从OSS下载文件
     * @param fid
     * @param response
     * @param session
     */
    @GetMapping("download/{fid}")
    @ApiOperation("从OSS下载文件")
    public void downloadFile(@PathVariable("fid")String fid, HttpServletResponse response,HttpSession session) {
        fileService.downloadFile(getUidFromSession(session),fid,response);
    }

    /**
     * 根据fid删除文件
     * @param fid
     * @return
     */
    @PostMapping("{fid}/delete")
    @ApiOperation("根据文件id删除文件")
    public JsonResult<Void> delete(@PathVariable("fid") String fid,HttpSession session) {
        fileService.delFileByFid(getUidFromSession(session),fid);
        return new JsonResult<>(OK);
    }

    /**
     * 保存文件夹
     * @param fileEntity
     * @param session
     * @return
     */
    @PostMapping("saveFloder")
    @ApiOperation("保存文件夹")
    public JsonResult<Void> saveFloder(FileEntity fileEntity, HttpSession session) {
        fileService.saveFloder(getUidFromSession(session),fileEntity);
        return new JsonResult<>(OK);
    }

    /**
     * 根据uid删除文件
     * @param uid
     * @return
     */
    @GetMapping("delFileByUid/{uid}")
    @ApiOperation("删除用户的所有文件")
    public JsonResult<String> delFileByUid(@PathVariable("uid") String uid) {
        return new JsonResult<>(OK,"成功删除该用户"+fileService.delFileByUid(uid)+"条文件");
    }

    /**
     * 移动文件
     * @param paramMap
     * @return
     */
    @PostMapping("moveFile")
    @ApiOperation("移动文件")
    public JsonResult<Void> moveFile(@RequestParam Map<String, String> paramMap, HttpSession session) {
        fileService.moveFile(getUidFromSession(session),paramMap);
        return new JsonResult<>(OK);
    }

    /**
     * 移动文件  判断目标目录是否已存在该文件名
     * @param list
     * @return
     */
    @PostMapping("moveFileRun")
    @ApiOperation("移动OSS文件")
    public JsonResult<Void> moveFileRun(@RequestBody ArrayList<String> list, HttpSession session) {
        fileService.moveFileRun(getUidFromSession(session),list);
        return new JsonResult<>(OK);
    }

    /**
     * 改变共享状态
     * @param paramMap
     * @param session
     * @return
     */
    @GetMapping("shareState")
    @ApiOperation("改变共享状态")
    public JsonResult<Void> shareState(@RequestParam Map<String, String> paramMap,HttpSession session) {
        //修改之前判断文件是否属于该用户 属于才能修改
        if (Objects.requireNonNull(getUidFromSession(session)).equals(paramMap.get("uid"))){
            fileService.changeShare(paramMap.get("uid"),paramMap);
            return new JsonResult<>(OK);
        }else {
            throw new FileStateException("权限不足!");
        }
    }

    /**
     * 共享列表
     * @param session share=00,自己的共享列表；share=03别人的共享列表
     * @return
     */
    @GetMapping("ShareList/{share}")
    @ApiOperation("获取共享列表")
    public JsonResult<List<FileEntity>> ShareList(@PathVariable("share") String share,HttpSession session) {
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("parUrl","/home");
        paramMap.put("share",share);
        return new JsonResult<>(OK,fileService.findList(getUidFromSession(session),paramMap));
    }

    /**
     * 文件或文件夹的重命名  修改文件名时OSS也要修改(OSS未提供文件重命名的接口,但可以用拷贝)
     * @param fileEntity
     * @param
     * @return
     */
    @PostMapping("rename")
    @ApiOperation("文件/文件夹重命名")
    public JsonResult<Void> rename(FileEntity fileEntity,HttpSession session) {
        fileService.renameByUid(getUidFromSession(session),fileEntity);
        return new JsonResult<>(OK);
    }

    public void test(){
        List<FileEntity> list = new ArrayList<>();

        //青铜
        for (int i = 0; i < list.size(); i++) fileService.saveFile(list.get(i));

        //白银
        for (FileEntity e : list) fileService.saveFile(e);

        //黄金
        list.forEach(e -> fileService.saveFile(e));

        //铂金
        list.forEach(fileService::saveFile);
    }
}
